package cn.tedu.vrd07.controller;

import cn.tedu.vrd07.entity.Product;
import cn.tedu.vrd07.entity.User;
import cn.tedu.vrd07.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/product/")
public class ProductController {
    @Autowired(required = false)
    ProductMapper mapper;

    @Value("${dirPath}")
    private String dirPath;

    @RequestMapping("insert")
    public int insert(Product product, MultipartFile picFile, HttpSession session) throws IOException {
        User user = (User) session.getAttribute("user");
        if(user==null){
            return 2;
        }

        System.out.println("product = " + product + ", picFile = " + picFile);
        String fileName = picFile.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID()+suffix;
        SimpleDateFormat format = new SimpleDateFormat("/yyyy/MM/dd/");
        String datePath = format.format(new Date());
        File dirFile = new File(dirPath+datePath);
        if(!dirFile.exists()){
            dirFile.mkdirs();
        }
        String filePath = dirPath+datePath+fileName;
        picFile.transferTo(new File(filePath));
        product.setUrl(datePath+fileName);
        product.setCreated(new Date());
        mapper.insert(product);
        return 1;
    }

    @RequestMapping("select")
    public List<Product> select(){
        return mapper.select();
    }

    @RequestMapping("delete")
    public void delete(int id){
        String url = mapper.selectUrlById(id);
        String filePath = dirPath+url;
        new File(filePath).delete();
        mapper.delete(id);
    }

    @RequestMapping("selectforindex")
    public List<Product> selectForIndex(){
        return mapper.selectForIndex();
    }

    @RequestMapping("selectByCid")
    public List<Product> selectByCid(int cid){
        return mapper.selectByCid(cid);
    }

    @RequestMapping("selectByKw")
    public List<Product> selectByKw(String kw){
        return mapper.selectByKw(kw);
    }

    @RequestMapping("selectView")
    public List<Product> selectView(){
        return mapper.selectView();
    }

    @RequestMapping("selectLike")
    public List<Product> selectLike(){
        return mapper.selectLike();
    }

    @RequestMapping("detail")
    public Product detail(int id,HttpSession session){
        String info = (String) session.getAttribute("view"+id);
        if(info==null){
            mapper.viewById(id);
            session.setAttribute("view"+id,"浏览过了!");
        }

        return mapper.detail(id);
    }

    @RequestMapping("like")
    public int like(int id,HttpSession session){
        String info = (String) session.getAttribute("like"+id);
        if(info==null){
            mapper.likeById(id);
            session.setAttribute("like"+id,"点过赞了!");
            return 1;
        }
        return 2;
    }
}
